import {simpleGet, simplePost} from "../../../utils/request";
import {getCacheOrder} from "../../../service/order";
import {getLoginUser} from "../../../service/user";
import {getDefaultAddress} from "../../../service/userAddress";

const app = getApp()
const global = app.globalData

Page({
    data: {
        nav:{
            title:"下单",
        },
        user:null,
        options:{},
        info:null,
        imageUrl:global.imageUrl,
        payLoading:false,
    },
    onLoad: function (options) {
        (async () => {
            const user = await getLoginUser()
            let order = getCacheOrder() // 客户选择的下单商品信息
            // 默认地地址
            const userAddress = await getDefaultAddress()
            if (userAddress){
                order.user_address_id = userAddress.id
                order.userAddress = userAddress
                // 到后台请求真实的订单信息
                // order = await this.getRealOrder(order)
            }

            console.log("order", order)

            this.setData({
                user,
                options:options||{},
                info:order    // 订单信息
            })
        })()
    },

    /**
     * 修改收货地址
     */
    changeAddress(){

        wx.navigateTo({
            url: `/pages/user/address/index?use_select=1`,
            events:{
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                selectUserAddressEvent: ({address}) => {
                    console.log("address", address)
                    this.setData({
                        [`info.address_id`]: address.id,
                        [`info.userAddress`]: address
                    })
                    // 更新订单信息
                    // this.updateRealOrder()
                },
            },
        })
    },

    numChange(e){
        const {value} = e.detail
        const {shopIndex, goodsIndex} = e.currentTarget.dataset
        console.log("numchange", e.detail)
        this.setData({
            [`info.shop_group.${shopIndex}.list[${goodsIndex}].num`]:value,
        })

        this.updateRealOrder()

    },

    /**
     * 切换支付方式
     * @param e
     */
    payTypeChange(e){
        const {value} = e.detail
        this.setData({
            [`info.pay_type`]:value,
        })
    },

    /**
     * 是否使用积分
     */
    pointsChange(e){
        const {value} = e.detail
        this.setData({
            [`info.is_use_points`]:value,
        })
    },

    /**
     * 备注
     */
    editNote(e){
        console.log("note",e)
        const {value} = e.detail
        const {index} = e.currentTarget.dataset

        this.setData({
            [`info.shop_group.${index}.user_note`]:value,
        })
    },
    payOrder(e){

        // return;

        // this.setData({
        //     payLoading:true,
        // })

        const {info} = this.data
        if (info.id){   // 已经创建订单，直接付款

        }else{  // 未创建订单
            simplePost("/order/create", info).then(r => {
                // todo:创建订单后直接进入结果页，缺少订单支付功能

                let url = '/pages/user/order_pay/pay_result'

                if (info.order_type == 'group' && info.is_only == 0){  // 拼团订单，且不是单独购买，进入拼团分享页
                    url = '/pages/user/active/group_share'
                }

                wx.reLaunch({
                    url,
                })

            })
        }

    },

    payOrderHandle(){

    },

    /**
     * 获取订单
     * 从后台请求新的订单信息，并返回
     * @param order
     * @returns {Promise<unknown>}
     */
    async getRealOrder(order){
        return await simplePost("/order/create_data", order)
    },
    /**
     * 更新订单
     * 从后台请求新的订单信息，并替换现有订单
     */
    updateRealOrder(){
        const {info} = this.data
       this.getRealOrder(info).then(r => {
           this.setData({
               info:r
           })
       })
    },

});
